﻿using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL.ProjectArtist;

namespace BLL.ProjectArtist
{
    public class ProjectArtistService : IProjectArtistService
    {
        private ProjectArtistDao projectArtistDao = new ProjectArtistDao();

        /// <summary>
        /// 应征需求，如果已经应征需求则取消应征
        /// </summary>
        /// <param name="projectId">需求id</param>
        /// <param name="ownerId">应征者id</param>
        /// <returns>true应征成功，反之则应征失败</returns>
        public bool Apply(int projectId, int ownerId)
        {
            if (!ExistProjectAstist(projectId, ownerId))
            {
                return Save(new Models.ProjectArtists() { OwnerID = ownerId, ProjectID = projectId });
            }
            else
            {
                return Delete(projectId, ownerId);
            }
        }

        /// <summary>
        /// 删除应征需求
        /// </summary>
        /// <param name="projectId">需求id</param>
        /// <param name="ownerId">应征者id</param>
        /// <returns>true为删除成功，反之则失败</returns>
        public bool Delete(int projectId, int ownerId)
        {
            return projectArtistDao.Delete(projectId, ownerId);
        }

        /// <summary>
        /// 判断该需求下，用户是否应征
        /// </summary>
        /// <param name="projectId">需求id</param>
        /// <param name="ownerId">判断者id</param>
        /// <returns>true则已经应征该需求，反之则无应征</returns>
        public bool ExistProjectAstist(int projectId, int ownerId)
        {
            return GetProjectAstistByProjectIdAndOwnerId(projectId, ownerId) != null;
        }

        /// <summary>
        /// 返回该用户下的所有已应征需求
        /// </summary>
        /// <param name="ownerId">用户id</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">每页显示多少条</param>
        /// <returns>需求应征实体类</returns>
        public List<ProjectArtists> GetListByOwnerId(int ownerId, int pageIndex = 1, int pageSize = 12)
        {
            List<Models.ProjectArtists> list = projectArtistDao.GetListByOwnerId(ownerId, pageIndex, pageSize);
            return list;
        }

        /// <summary>
        /// 返回该需求下所有已应征的用户
        /// </summary>
        /// <param name="projectId">需求id</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">每页显示多少条</param>
        /// <returns>需求应征实体类</returns>
        public List<ProjectArtists> GetListByProjectId(int projectId, int pageIndex = 1, int pageSize = 12)
        {
            List<Models.ProjectArtists> list = projectArtistDao.GetListByProjectId(projectId, pageIndex, pageSize);
            return list;
        }

        /// <summary>
        /// 通过需求id和应征者id，返回该应征需求实体类
        /// </summary>
        /// <param name="projectId">需求id</param>
        /// <param name="ownerId">应征者id</param>
        /// <returns>需求应征实体类</returns>
        public ProjectArtists GetProjectAstistByProjectIdAndOwnerId(int projectId, int ownerId)
        {
            return projectArtistDao.GetProjectAstistByProjectIdAndOwnerId(projectId, ownerId);
        }




        /// <summary>
        /// 返回该需求总和
        /// </summary>
        /// <returns>总和</returns>
        public int GetTotal()
        {
            return projectArtistDao.GetTotal();
        }

        /// <summary>
        /// 应征需求
        /// </summary>
        /// <param name="projectArtist">应征需求实体类</param>
        /// <returns>true则应征成功，反之则应征失败</returns>
        public bool Save(ProjectArtists projectArtist)
        {
            return projectArtistDao.Insert(projectArtist);
        }
    }
}
